<template>
  <!-- 导航栏 -->
  <header class="header">
    <div class="nav-container">
      <div class="logo-nav-wrapper">
        <router-link to="/" class="logo">Pink</router-link>
        <ul class="nav-menu" v-for="item in navMenuList" :key="item.id">
          <li><router-link :to="item.href">{{item.name}}</router-link></li>
        </ul>
      </div>

      <div class="search-box-wrapper">
        <div class="search-box">
          <input type="text" name="serve" placeholder="搜索动漫、UP主...">
          <button>搜索</button>
        </div>
      </div>

      <div class="user-actions">
        <router-link to="/animeHistory" class="history-btn">
          <i class="fas fa-history"></i>
          <span>播放历史</span>
        </router-link>
        <div class="login-btn" v-if="token === null||token === ''">
          <i class="fas fa-user"></i>
          <span @click="showLoginModal = true">登录</span>
        </div>
        <div class="login-main" v-else>
        <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
          <AvatarImg :size="32" :src="userInfoPink.avatarUrl" />
          <span>{{userInfoPink.username}}</span>
          <el-icon><arrow-down /></el-icon>
        </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="个人中心">个人中心</el-dropdown-item>
                <el-dropdown-item command="退出登录" divided>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </div>
  </header>
  <LoginModal
      :show="showLoginModal"
      @close="showLoginModal = false"
      @login-success="handleLoginSuccess"
      @register-success="handleRegisterSuccess"
  />
</template>
<script setup>
 import { ref } from 'vue';
 import LoginModal from "@/views/components/account/LoginModal.vue";
 import { userInform } from "@/stores/userInform.js";
 import {logout} from "@/api/acconut.js";
 import router from "@/router/index.js";
 import AvatarImg from "@/components/AvatarImg.vue";

  const userInfoPink = userInform()

 const navMenuList = [
   {id: 0,name:'首页',href:'/'},
   {id: 1,name:'番剧',href:'/animeFilter'},
   {id: 2,name:'国创',href:'/'},
   {id: 3,name:'电影',href:'/MovieFilter'},
   {id: 4,name:'专栏',href:'/'}
 ]

 const showLoginModal = ref(false)
 let token = userInfoPink.token
 const handleLoginSuccess = (userInfo) => {
   window.location.reload();
 }

 const handleRegisterSuccess = (userInfo) => {
   console.log('注册成功:', userInfo)
   // 处理注册成功逻辑
 }

 const handleCommand = async (command) => {
   if(command === '个人中心') {
     router.push('/user');
   }
   if (command === '退出登录') {
     // 执行退出登录操作，例如清除token等
     await logout()
     userInfoPink.clearAll();
     localStorage.removeItem('token');
     //js跳转
     window.location.reload();
   }
 };
</script>
<style lang="scss" scoped>
/* 导航栏样式 */
.header {
  background-color: #3498db;
  padding: 12px 0;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-container {
  max-width: 1400px;
  min-width: 800px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo-nav-wrapper {
  display: flex;
  align-items: center;
  flex: 1;
}

.logo {
  font-size: 20px;
  font-weight: bold;
  color: white;
  text-decoration: none;
  margin-right: 30px;
}

.nav-menu {
  display: flex;
  list-style: none;
  min-width: 50px;
}

.nav-menu li {
  margin-right: 20px;
}

.nav-menu a {
  color: white;
  text-decoration: none;
  font-size: 14px;
  transition: all 0.3s;
}

.nav-menu a:hover {
  color: #f1c40f;
}

.search-box-wrapper {
  flex: 1;
  display: flex;
  justify-content: center;
}

.search-box {
  display: flex;
  align-items: center;
  width: 100%;
  max-width: 300px;
}

.search-box input {
  padding: 8px 15px;
  border: none;
  border-radius: 20px 0 0 20px;
  outline: none;
  width: 100%;
}

.search-box button {
  padding: 8px 15px;
  width: 80px;
  background-color: #2980b9;
  color: white;
  border: none;
  border-radius: 0 20px 20px 0;
  cursor: pointer;
}

.user-actions {
  display: flex;
  align-items: center;
  flex: 1;
  justify-content: flex-end;
}

.history-btn, .login-btn {
  color: white;
  text-decoration: none;
  font-size: 14px;
  margin-left: 20px;
  display: flex;
  align-items: center;
}

.history-btn i, .login-btn i {
  margin-right: 5px;
}

.login-main {
  margin-left: 15px;
  display: flex;
  align-items: center;
  .el-dropdown-link {
    display: flex;
    align-items: center;
    cursor: pointer;
  }

  .el-dropdown-link span {
    margin: 0 5px;
    color: white;
  }
}
</style>